package com.imooc.controller;

import com.imooc.base.BaseInfoProperties;
import com.imooc.entity.pojo.Users;
import com.imooc.grace.result.GraceJSONResult;
import com.imooc.grace.result.ResponseStatusEnum;
import com.imooc.service.FansService;
import com.imooc.service.MsgService;
import com.imooc.service.UsersService;
import com.imooc.utils.PagedGridResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/fans")
@Slf4j
public class FansController extends BaseInfoProperties {
    @Autowired
    private FansService fansService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private MsgService msgService;
    @PostMapping("/follow")
    public GraceJSONResult doFollow( @RequestParam("myId")String myId, @RequestParam("vlogerId")String vlogerId){
        if(myId==null||vlogerId==null){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.UN_LOGIN);
        }
        Users me = usersService.findUserByUserId(myId);
        Users vloger = usersService.findUserByUserId(vlogerId);
        if(me==null||vloger==null){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_NOT_EXIST);
        }
        if(me==vloger){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.NO_FOLLOW_YOURSELF);
        }
        fansService.doFollow(myId,vlogerId);
        // 博主粉丝+1 我的关注+1
        redis.increment(REDIS_MY_FOLLOWS_COUNTS+":"+myId,1);
        redis.increment(REDIS_MY_FANS_COUNTS+":"+vlogerId,1);
        // 标记我和博主的关系
        redis.set(REDIS_USER_AND_VLOGGER_RELATIONSHIP+":"+myId+":"+vlogerId,"1");
        return GraceJSONResult.ok();
    }
    @PostMapping("/cancel")
    public GraceJSONResult doCancelFollow(@RequestParam("myId")String myId,
                                          @RequestParam("vlogerId")String vlogerId){
        if(myId==null||vlogerId==null){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.UN_LOGIN);
        }
        Users me = usersService.findUserByUserId(myId);
        Users vloger = usersService.findUserByUserId(vlogerId);
        if(me==null||vloger==null){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_NOT_EXIST);
        }
        if(me==vloger){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.NO_FOLLOW_YOURSELF);
        }
        // 取消关注
//        fansService.doFollow(myId,vlogerId);
        log.info("删除关系 取消关注:myId:{},对方Id:{}",myId,vlogerId);
        fansService.doCancel(myId,vlogerId);
        // 博主粉丝-1 我的关注-1
        redis.decrement(REDIS_MY_FOLLOWS_COUNTS+":"+myId,1);
        redis.decrement(REDIS_MY_FANS_COUNTS+":"+vlogerId,1);
        // 删除我和博主的关系
        redis.del(REDIS_USER_AND_VLOGGER_RELATIONSHIP+":"+myId+":"+vlogerId);
        msgService.unFollowVlogger(myId,vlogerId);
        return GraceJSONResult.ok();
    }
    @GetMapping("/queryDoIFollowVloger")
    public GraceJSONResult queryDoIFollowVloger(@RequestParam("myId")String myId,
                                                @RequestParam("vlogerId")String vlogerId) {
        return GraceJSONResult.ok(fansService.queryDoIFollowVloger(myId, vlogerId));
    }
    @GetMapping("/queryMyFollows")
    public GraceJSONResult queryMyFollow(@RequestParam("myId")String myId,@RequestParam("page")Integer page,@RequestParam("pageSize")Integer pageSize){
        return GraceJSONResult.ok(fansService.queryMyFollow(myId,page,pageSize));
    }
    @GetMapping("/queryMyFans")
    public GraceJSONResult queryMyFans(@RequestParam("myId")String myId,@RequestParam("page")Integer page,@RequestParam("pageSize")Integer pageSize){
        return GraceJSONResult.ok(fansService.queryMyFans(myId,page,pageSize));
    }
}
